package com.herui.iot.ui.report;

import com.herui.sdyu_lib.http.callback.Callback;
import com.herui.sdyu_lib.utils.StringUtils;
import com.herui.sport_care.BasePresenter;
import com.herui.sport_care.BaseView;
import com.herui.sport_care.data.RepositoryFactory;
import com.herui.sport_care.data.report.EvaluatorAppEvaluationReportResponse;
import com.herui.sport_care.data.report.EvaluatorAppQueryEvaluationReportSingleResp;
import com.herui.sport_care.data.report.Report;
import com.herui.sport_care.data.report.ReportUser;
import com.herui.sport_care.data.report.TrendData;
import com.herui.sport_care.data.report.source.ReportSource;
import com.herui.sport_care.data.user.UserInfo;
import com.herui.sport_care.request.SortType;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by amanda.shan on 2022/12/8.
 */
public class ReportPresenter implements BasePresenter {

    static final String TAG = "EvalPresenter";

    public static interface View extends BaseView {
        // 回调当日评估列表
        default void onReport(List<Report> result) {
        }

        // 返回用户列表
        default void onReportUser(List<ReportUser> reportUsers) {
        }

        // 单项报告详情
        default void onEvaluatorAppQueryEvaluationReportSingleResp(EvaluatorAppQueryEvaluationReportSingleResp resp) {
        }

        // 综合报告详情
        default void onEvaluatorAppEvaluationReportResponse(EvaluatorAppEvaluationReportResponse resp) {
        }

        //
        default void onTrendData(TrendData trendData){}
        // 回调编辑报告成功
        default void onUpdate(){}
    }

    private View view;
    private ReportSource reportSource;

    public ReportPresenter(View view) {
        this.view = view;
        reportSource = RepositoryFactory.getInstance().getReportSource();
    }

    @Override
    public void start() {

    }

    /**
     * 获取报告列表
     *
     * @param pageNumber
     * @param organId
     * @param agreementId
     * @param beEvaluatedName
     * @param betaSchemeId
     * @param sortType
     */
    public void getReportList(int pageNumber, Long organId, Long agreementId,
                              String beEvaluatedName, Long betaSchemeId, SortType sortType) {
        UserInfo userInfo = RepositoryFactory.getInstance().getUserSource().getUserInfo();
        Map<String, Object> map = new HashMap<>();
        map.put("pageNumber", pageNumber);
        map.put("pageSize", 10);

        if (organId != null) {
            map.put("organId", organId);
        }
        if (agreementId != null) {
            map.put("agreementId", agreementId);
        }
        if (!StringUtils.isEmpty(beEvaluatedName)) {
            map.put("beEvaluatedName", beEvaluatedName);
        }
        if (betaSchemeId != null) {
            map.put("betaSchemeId", betaSchemeId);
        }
        if (sortType != null) {
            map.put("evaluationDateSort", sortType);
        }
        reportSource.getReportList(map, new Callback.CommonCallback<List<Report>>() {
            @Override
            public void onSuccess(List<Report> result) {
                view.onReport(result);
            }

            @Override
            public void onError(Throwable e, String code, String message) {
                processError(e, code, message);
            }
        });
    }

    /**
     * 单项或综合报告下的人员列表
     *
     * @param evaluationSchemeId 评估id
     */
    public void getReportUserList(Long evaluationSchemeId) {
        reportSource.getReportUserList(evaluationSchemeId,
                new Callback.CommonCallback<List<ReportUser>>() {
            @Override
            public void onSuccess(List<ReportUser> result) {
                view.onReportUser(result);
            }

            @Override
            public void onError(Throwable e, String code, String message) {
                processError(e, code, message);
            }
        });
    }

    /**
     * 获取单项报告详情
     * @param id
     */
    public void getEvaluationReportSingle(Long id) {
        reportSource.getEvaluationReportSingle(id,
                new Callback.CommonCallback<EvaluatorAppQueryEvaluationReportSingleResp>() {
            @Override
            public void onSuccess(EvaluatorAppQueryEvaluationReportSingleResp result) {
                view.onEvaluatorAppQueryEvaluationReportSingleResp(result);
            }

            @Override
            public void onError(Throwable e, String code, String message) {
                processError(e, code, message);
            }
        });
    }

    /**
     * 获取综合报告详情
     * @param id
     */
    public void getEvaluationReportAll(Long id) {
        reportSource.getEvaluationReportAll(id,
                new Callback.CommonCallback<EvaluatorAppEvaluationReportResponse>() {
            @Override
            public void onSuccess(EvaluatorAppEvaluationReportResponse result) {
                view.onEvaluatorAppEvaluationReportResponse(result);
            }

            @Override
            public void onError(Throwable e, String code, String message) {
                processError(e, code, message);
            }
        });
    }

    /**
     * 综合报告趋势图
     * @param id 评估数据id
     */
    public void getTrendData(Long id) {
        reportSource.getTrendData(id,
                new Callback.CommonCallback<TrendData>() {
                    @Override
                    public void onSuccess(TrendData result) {
                        view.onTrendData(result);
                    }

                    @Override
                    public void onError(Throwable e, String code, String message) {
                        processError(e, code, message);
                    }
                });
    }

    /**
     * 编辑报告
     * @param map
     */
    public void updateEvaluationReport(Map<String, Object> map) {
        reportSource.updateEvaluationReport(map,
                new Callback.CommonCallback<String>() {
                    @Override
                    public void onSuccess(String result) {
                        view.onUpdate();
                    }

                    @Override
                    public void onError(Throwable e, String code, String message) {
                        processError(e, code, message);
                    }
                });
    }

    @Override
    public BaseView getView() {
        return view;
    }
}
